package com.ijiuchuang.jc_localization.view.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.ijiuchuang.jc_localization.R;
import com.ijiuchuang.jc_localization.base.BaseActivity;
import com.ijiuchuang.jc_localization.bean.cache.TagCache;
import com.ijiuchuang.jc_localization.bean.lite.Station;
import com.ijiuchuang.jc_localization.contract.PositionContract;
import com.ijiuchuang.jc_localization.presenter.PositionPresenter;
import com.ijiuchuang.jc_localization.tools.views.MapView;
import com.ijiuchuang.jc_localization.view.adapter.SocketListRecyclerAdapter;

import java.net.Socket;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PositionActivity extends BaseActivity implements PositionContract.View {

    @BindView(R.id.position_toolbar)
    Toolbar toolbar;
    @BindView(R.id.position_map_view)
    MapView mapView;
    @BindView(R.id.position_fab)
    FloatingActionButton fab;
    @BindView(R.id.position_coordinator)
    CoordinatorLayout coordinator;

    private PositionContract.Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_position);
        ButterKnife.bind(this);

        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        presenter = new PositionPresenter(this, this);

    }

    @Override
    protected void onStart() {
        super.onStart();

        presenter.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_position, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch (id) {
            case R.id.menu_position_map:
                // pink : later to do sth.
                return true;
            case R.id.menu_position_connection_list:
                presenter.getSocketList();
                return true;
            case R.id.menu_position_settings:
                Intent intentSetts = new Intent(this, FragmentActivity.class);
                intentSetts.putExtra(FragmentActivity.FRAGMENT_NAME, FragmentActivity.FRAGMENT_NAME_SETTS_LOCATION);
                startActivity(intentSetts);
                return true;
            case R.id.menu_position_device_list:
                Intent intentDevices = new Intent(this, FragmentActivity.class);
                intentDevices.putExtra(FragmentActivity.FRAGMENT_NAME, FragmentActivity.FRAGMENT_NAME_DEVICE_LIST);
                startActivity(intentDevices);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @OnClick(R.id.position_fab)
    public void onClick() {
        Snackbar.make(coordinator, "是否建立Socket连接", Snackbar.LENGTH_LONG)
                .setAction("OK", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        presenter.socketConnect();
                    }
                })
                .show();
    }

    @Override
    public void setPresenter(PositionContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void initMap() {
        mapView.clearStations();
        mapView.setPointShown(false);
        mapView.setTipsShown(false);
        mapView.setMagnifyingShown(false);
    }

    @Override
    public void setMap(Bitmap bitmap) {
        mapView.setImageBitmap(bitmap);
    }

    @Override
    public void addStations(Station... stations) {
        mapView.setStations(stations);
    }

    @Override
    public void showConnection(List<Socket> list) {
        View view = View.inflate(this, R.layout.dialog_one_recycler, null);
        RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.dialog_one_recycler);

        SocketListRecyclerAdapter mRecyclerAdapter = new SocketListRecyclerAdapter(this, R.layout.list_item_socket, list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mRecyclerAdapter);

        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle(R.string.dialog_title_info)
                .setView(view)
                .setPositiveButton(R.string.dialog_btn_ok, null)
                .create();
        dialog.show();
    }

    @Override
    public void setLocationPoint(PointF point) {
        mapView.setLocationPoint(point);
    }

    @Override
    public void setLocationPoint(String name, PointF point) {
        mapView.setLocationPointWithName(name, point);
    }
}
